<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/8/31 20:59
 */

namespace module\chat\logic;

use module\chat\model\Role;
use module\user\library\Auth;
use module\user\logic\FavoriteLogic;
use mof\annotation\Inject;
use mof\Logic;
use mof\Request;
use mof\Searcher;

class RoleLogic extends Logic
{
    public function search(Auth $auth, Request $request): \think\Paginator
    {
        $params = ['status' => 1];
        if ($request->has('category_id')) {
            $params['category_id'] = $request->get('category_id');
        }
        if ($request->has('keyword')) {
            $params['name'] = $request->get('keyword');
        }
        $paginate = (new Searcher())
            ->model($this->model::class)
            ->with(['category'])
            ->order(['listorder' => 'desc'])
            ->field(['id', 'category_id', 'name', 'icon', 'intro', 'favor_ct', 'used_ct'])
            ->order(['listorder' => 'desc'])
            ->pageSize(min($request->get('pageSize', 10), 300))
            ->params($params)
            ->paginate(true);

        if ($paginate->count() && $auth->isLogin()) {
            $favorLogic = app(FavoriteLogic::class);
            $favorLogic->appendStatus($auth->getId(), $paginate->getCollection());
        }

        return $paginate;
    }

    public function favorites(Auth $auth, Request $request)
    {
        $logic = app(FavoriteLogic::class);
        $paginate = $logic->userSourcesPaginate(
            $auth->getId(),
            $this->model->newInstance(),
            $request->get('keyword', '')
        );
        //增加分类属性
        $paginate->getCollection()->load(['category']);
        return $paginate;
    }
}